package com.ym.manager.admin.goods;

import com.ym.manager.admin.goods.model.Goods;
import com.ym.manager.admin.goods.model.GoodsStatusTotal;
import com.ym.utils.Common;
import com.ym.utils.DateUtil;
import com.ym.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: ym_manager->GoodsService
 * @Auther: 但何非
 * @Date: 2019/11/29 16:37
 * @Description:
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    public static String sourceUrl = "https://www.ys168.vip/";
    public static String picUrl = sourceUrl + "images/uploads/";

    /**
     * 商品列表数据
     * @param pageNum
     * @param limit
     * @param merchant_id
     * @param name
     * @param categoryPid
     * @param categoryId
     * @param status
     * @return
     */
    public Pages<Goods> getList(int pageNum, int limit,int merchant_id,String name,int categoryPid,int categoryId,int status){
        Map<String,Object> map = new HashMap<>();
        int begin = (pageNum-1)*limit;
        map.put("begin",begin);
        map.put("limit",limit);
        map.put("merchant_id",merchant_id);
        map.put("name",name);
        map.put("categoryPid",categoryPid);
        map.put("categoryId",categoryId);
        map.put("status",status);
        int count  = goodsDao.getListCount(map);
        List<Goods> list = goodsDao.getList(map);
        Pages<Goods> pages = new Pages<>(count,pageNum,limit);
        pages.setList(list);
        return pages;
    }

    /**
     * 新增商品
     * @param goods
     * @return
     */
    public int insert(Goods goods){
        int i = 0;
        try {
//            goods.setIdx(goodsDao.getMaxIdx());
            i = goodsDao.insert(goods);
        }catch (Exception e){
            i = -1;
            e.printStackTrace();
        }
        return i;
    }
    public long getMaxIdx(){
        return goodsDao.getMaxIdx();
    }
    /**
     * 修改商品
     * @param g
     * @return
     */
    public int update(Goods g){
        int i = 0;
        try {
            g.picUrlDel(g,picUrl);
            i = goodsDao.update(g);
        }catch (Exception e){
            i = -1;
            e.printStackTrace();
        }
        return i;
    }

    public int delete(int id){
        int i = 0;
        try {
            i = goodsDao.deleteByPrimaryKey(id);
        }catch (Exception e){
            i = -1;
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 获取各个状态的产品总数
     * @return
     */
    public GoodsStatusTotal goodsStatusTotal(int hospital_id) {
        List<Map<String,Object>> list = goodsDao.goodsStatusTotal(hospital_id);
        return new GoodsStatusTotal(list);
    }

    /**
     * 根据id获取机构详情
     * @param id
     * @return
     */
    public Goods detail(int id) {
        Goods detail = goodsDao.detail(id);
        detail.picUrlChange(detail);
        return detail;
    }

    /**
     * 功能：上下架产品
     * @param id
     * @param shelf
     * @return
     */
    public int shelf(int id, int shelf) {
        return goodsDao.shelf(id,shelf);
    }

    /**
     * 根据机构id获取项目列表
     * @param hospital_id
     * @return
     */
    public List<Goods> goodsList(int hospital_id) {
        return goodsDao.goodsList(hospital_id);
    }

    /**
     * 根据类目机构id获取项目列表
     * @param hospital_id
     * @param categoryPid
     * @param categoryId
     * @return
     */
    public List<Goods> getCategoryGoodsList(int hospital_id, int categoryPid, int categoryId) {
        return goodsDao.getCategoryGoodsList(hospital_id,categoryPid,categoryId);
    }

    /**
     * 获取用户的佣金比例值
     * @return
     */
    public int getUserRate(){
        Integer rate = goodsDao.getUserRate();
        if(Common.isEmpty(rate) || rate==0){
            return 70;
        }
        return rate;
    }

    /**
     * 商品入库
     * @param goods_id 商品id
     * @param validity 有效期
     * @param inventory 库存
     * @return
     */
    @Transactional
    public int addInventory(int goods_id, String validity, int inventory) {
        //增加商品库存
        goodsDao.addGoodsInventory(goods_id,inventory);
        //增加库存表数据
        return goodsDao.addInventory(goods_id,validity,inventory);
    }

    /**
     * 加入秒杀
     * @param goods_id
     * @param unit
     * @param time
     * @return
     */
    @Transactional
    public int addSeckill(int goods_id, String unit,String time) {
        //复制商品数据
        Goods detail = goodsDao.detail(goods_id);
        detail.setType(2);
        detail.setUnits(unit);
        String now = DateUtil.getDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        detail.setUpdate_time(now);
        detail.setCreate_time(now);
        //获取商品库存信息
        int inventory = goodsDao.getInventory(goods_id,time);
        detail.setInventory(inventory);
        //记录商品信息
        goodsDao.insert(detail);
        //商品入库
        goodsDao.addGoodsInventory(detail.getId(),inventory);
        return 1;
    }

    /**
     * 根据商品id获取入库有效期集
     * @param goods_id
     * @return
     */
    public List<String> getSeckillTime(int goods_id) {
        String now = DateUtil.getDate(new Date(),"yyyy-MM-dd");
        return goodsDao.getSeckillTime(goods_id,now);
    }
}
